#pragma once

#include <SFML/Graphics.hpp>
#include <vector>

#include "IGameState.h"
#include "UnitFactory.h"
#include "KeyLock.h"
#include "SpriteSheetManager.h"
#include "Terrain.h"
//entity manager
#include <vector>

namespace game
{	


class GameStateManager
{
	sf::RenderWindow *			_renderWindow;
	game::KeyLock *				_inputManager;
	game::SpriteSheetManager*	_spriteSheetManager;
	
	std::vector<IGameState*>	_states;

	//battle specific			//Needs refactoring into PlayingState//
	game::TerrainManager *		_terrain_manager;
	game::UnitFactory *			_unit_factory;

	

public:
	sf::RenderWindow *			getRenderWindow(){return _renderWindow;}
	game::KeyLock *				getInput(){return _inputManager;}
	game::SpriteSheetManager *	getSpriteSheetManager(){return _spriteSheetManager;}
	game::TerrainManager *		getTerrainManager(){return _terrain_manager;}
	game::UnitFactory *			get_unit_factory(){return _unit_factory;}

	~GameStateManager()
	{
		const int size = _states.size();
		for (int i = 0; i < size; i++)
		{
			if (_states.at(i))
			{
				delete _states.at(i);
			}
		}
	};

	GameStateManager(sf::RenderWindow *rw,
		game::KeyLock * input,
		game::SpriteSheetManager * ssm,
		game::TerrainManager * tm,
		game::UnitFactory * uf)
	{
		_terrain_manager = tm;
		_spriteSheetManager = ssm;
		_renderWindow = rw;
		_inputManager = input;
		_unit_factory = uf;
	}

	void Load()
	{
		for each (IGameState * gs in _states)
		{
			if (!gs->isLoaded())
			{
				std::cout<<"Loading gs" << std::endl;
				gs->Load();
			}
			if (!gs->isTransparent()){break;}
		}
	}
	void Update()
	{
		for each (IGameState * gs in _states)
		{
			gs->Update();
			if (!gs->isTransparent()){break;}
		}
	}
	void Draw()
	{
		for each (IGameState * gs in _states)
		{
			gs->Draw();
			if (!gs->isTransparent()){break;}
		}
	}
	void ChangeState(IGameState * gameState)
	{
		_states.at(0) = gameState;
		Load();
	}
	void AddState(IGameState * gameState)
	{
		_states.push_back(gameState);
		Load();
	}
};
}